package com.example.demo.controller;

import com.example.demo.common.domain.Employee;
import com.example.demo.common.domain.Record;
import com.example.demo.common.util.Result;
import com.example.demo.service.DateService;
import com.example.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class RecordController {

    @Autowired
    @Qualifier("ds")
    private DateService dateService;

    @Autowired
    @Qualifier("recordServiceImpl")
    private RecordService recordService;

    public boolean check(HttpSession session){
        if(session.getAttribute("employee")!=null)
            return true;
        else
            return false;
    }


    @RequestMapping("/cal")//用户点击进入本页面前，动态产生本月的日历并渲染到前端
    public String cal(HttpSession session, Model model){
        if(check(session)){
            Integer[] daysTable = dateService.getCalendarTable();
            model.addAttribute("daysTable",daysTable);
            model.addAttribute("month",dateService.getMonth());
            model.addAttribute("day",dateService.getDay());
            model.addAttribute("dayInWeek",dateService.getDayInWeek());
            model.addAttribute("year",dateService.getYear());

            return "cal";
        }else{
            return "login";
        }

    }
    @RequestMapping("/getRecords")
    @ResponseBody//通过点击本月的某一天，根据用户ID和日期，对打卡日期进行搜索，返回结果渲染到前端
    public Map getRecords(HttpSession session,int day){
        int eid = ((Employee)session.getAttribute("employee")).getEid();
        Record record = recordService.selectByEidDateEq(eid, day);


        if(record == null){
            return Result.success(true).put("len",0);
        }

        System.out.println(record);
        return Result.success(true)
                .put("sbTime",record.getRstarttime())
                .put("xbTime",record.getRendtime())
                .put("isAbsent",record.getIsabsent())
                .put("len",3);
    }
}
